/*
 * UINumTextfield.java
 *
 * Created on 14 maart 2007, 9:33
 *
 * This file is part of SimpLie.
 * 
 * SimpLie is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * SimpLie is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with SimpLie.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package edu.simplie.ui.reusable;

import edu.simplie.Helper;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import org.jdesktop.application.Action;

/**
 *
 * @author  Teake Nutma
 * @version $Revision: 349 $, $Date: 2008-05-13 09:00:06 +0000 (Tue, 13 May 2008) $
 */
public class UINumTextfield extends javax.swing.JPanel
{
	private int minValue = Integer.MIN_VALUE;
	private int maxValue = Integer.MAX_VALUE;
	
	/**
	 * Creates new form UINumTextfield
	 */
	public UINumTextfield()
	{
		initComponents();
		
		tfLevel.addKeyListener(new KeyListener()
		{
			public void keyPressed(KeyEvent e)
			{
			}
			public void keyReleased(KeyEvent e)
			{
			}
			public void keyTyped(KeyEvent e)
			{
				int k=e.getKeyChar();
				if((k>47 && k<58)|| k==8 || k==KeyEvent.VK_MINUS)
				{
				}
				else
				{
					e.setKeyChar((char)KeyEvent.VK_CLEAR);
				}
			}
		});
	}
	
	public void setText(String text)
	{
		textField.setText(text);
	}
	public int getValue()
	{
		return Helper.stringToInt(tfLevel.getText());
	}
	
	public void setValue(int value)
	{
		value = Math.max(value,minValue);
		value = Math.min(value,maxValue);
		tfLevel.setText(Helper.intToString(value));
	}
	
	public void setMinValue(int value)
	{
		minValue = Math.min(maxValue,value);
	}
	
	public void setMaxValue(int value)
	{
		maxValue = Math.max(minValue,value);
	}
	

	
	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        textField = new javax.swing.JLabel();
        tfLevel = new javax.swing.JTextField();
        buttonPlus = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        textField.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        textField.setText("<text>:");

        tfLevel.setText("0");

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(edu.simplie.SimpLieApp.class).getContext().getActionMap(UINumTextfield.class, this);
        buttonPlus.setAction(actionMap.get("doIncrease")); // NOI18N

        jButton1.setAction(actionMap.get("doDecrease")); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(textField)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(tfLevel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 31, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(buttonPlus)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jButton1))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                .add(textField)
                .add(tfLevel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(buttonPlus)
                .add(jButton1))
        );
    }// </editor-fold>//GEN-END:initComponents

	@Action
	public void doIncrease()
	{
		setValue(getValue()+1);
	}

	@Action
	public void doDecrease()
	{
		setValue(getValue()-1);
	}
	
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonPlus;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel textField;
    private javax.swing.JTextField tfLevel;
    // End of variables declaration//GEN-END:variables
	
}
